<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";

if ($loadsubpage == 1)
{
  $doctype = isset ($_GET['dt']) ? mysql_real_escape_string ($_GET['dt']) : "";

  /* Get site name */
  $result = mysql_query ("SELECT sitename, domainname, protocol FROM ".db_maketablename ($table_settings)." LIMIT 1");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      $sitename = htmlentities ($row['sitename'], ENT_COMPAT, "UTF-8", false);
      $domainname = htmlentities ($row['domainname'], ENT_COMPAT, "UTF-8", false);
      $protocol = $row['protocol'] != "" ? $row['protocol'] : "http";
    }
  mysql_free_result ($result);
?>
<h1>
  <?=$sitename?>
  <a href="<?=$protocol?>://<?=$domainname?>/" title="Go to website" rel="external"><img src="./images/link.gif" alt="CodeMirror" class="externallink" /></a>
</h1>

<div id="doctypes">
  <h3>File types</h3>
<?php
asort ($doctypes);
foreach ($doctypes as $key => $type)
  print "&bull; <a href=\"?p=files&amp;dt=$key\">$type</a><br />\n";
?>
  <div id="addnew">
    <p>
      <input type="button" value="+" class="button" onclick="opendialog ('editfile.php?new', 600, 400);" />
      Add new file
    </p>
  </div>
</div>

<div id="browser">
<?php
/* If no page is selected... */
if (empty ($doctype))
{
?>
  <div id="info">
    <p>&larr; Click on a file type to browse the library.</p>
  </div>
<?php
} /* if (empty ($doctype)) */
else
{
?>
  <div id="tabs">
    <ul>
      <li><span>Browser</span></li>
    </ul>
  </div>
<?php
$result = mysql_query ("SELECT id, filename, filename_orig, name, comment FROM ".db_maketablename ($table_files)." WHERE doctype='$doctype' ORDER BY filename_orig");
if (mysql_num_rows ($result) > 0)
{
  while ($row = mysql_fetch_array ($result))
    {
      print "<table class=\"fileview\">\n";
      print "<tr><td colspan=\"3\" style=\"padding-bottom: 2px;\"><h4>".htmlentities (stripslashes ($row['filename_orig']), ENT_COMPAT, "UTF-8", false)."</h4></td></tr>\n";
      
      print "<tr>";
      
      $file = "$app_filespath/{$row['filename']}";
      if ($doctype == 1) /* Image */
        {
          list ($width, $height) = getimagesize ("{$_SERVER['DOCUMENT_ROOT']}$file");
          if ($width > 150)
            {
              $newwidth = 150;
              $difftoheight = (($width - $newwidth) * 100) / $width;
              $newheight = round ($height - ($height * $difftoheight / 100));
              
              $width = $newwidth;
              $height = $newheight;
              if ($height > 50)
                {
                  $newheight = 50;
                  $difftowidth = (($height - $newheight) * 100) / $height;
                  $newwidth = round ($width - ($width * $difftowidth / 100));
                }
            }
          else
            {
							$newheight = $height;
					    $newwidth = $width;
						}
          print "<td style=\"width: 150px; padding-right: 5px;\" class=\"aligntop\"><img src=\"$file\" style=\"border: none; height: ".$newheight."px; width: ".$newwidth."px;\" alt=\"\" /></td>\n";
        }
      
      $comment = !empty ($row['comment']) ? htmlentities (stripslashes ($row['comment']), ENT_COMPAT, "UTF-8", false) : "<em>N/A</em>";
      print "<td style=\"padding-right: 5px;\" class=\"aligntop\"><strong>".htmlentities (stripslashes ($row['name']), ENT_COMPAT, "UTF-8", false)."</strong><br/>$comment</td>\n";
      print "<td class=\"alignright operate\" style=\"vertical-align: top;\">";
      print "<input type=\"button\" value=\"Edit\" class=\"button btnstdsize space\" onclick=\"opendialog ('editfile.php?fid={$row['id']}', 600, 400);\" /><br />";
      print "<input type=\"button\" value=\"Delete\" class=\"button btnstdsize space\" onclick=\"delfile ('{$row['filename_orig']}', {$row['id']}, {$_GET['dt']});\" />";
      print "</td></tr>\n";
        
      print "</table>\n";
    }
}
mysql_free_result ($result);
?>
<?php
} /* if (empty ($doctype)) */
?>

</div>
<div class="clear"></div>
<?php
}
?>
